package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.itemView.SettingItemView;

import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.pattern.orderOprate.OrderOperateCallback;
import com.tianhui.consignortwofei.pattern.orderOprate.OrderRequestOperate;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/28
 * @description
 **/
public class ConfirmDocumentActivity extends BaseActivity {




//    @BindView(R.id.layout_operate_tip_dialog_positiveTextView)
//    private TextView positiveTextView;
//    @BindView(R.id.layout_operate_tip_dialog_negativeTextView)
//    private TextView negativeTextView;
    @BindView(R.id.siv_daohuo_time)
    SettingItemView siv_daohuo_time;
    @BindView(R.id.dialog_order_share_tipTextView)
    EditText tipTextView;
    @BindView(R.id.dialog_order_share_contextTextView)
    EditText contentTextView;

    @BindView(R.id.et_siji_phone)
    EditText et_siji_phone;
    @BindView(R.id.et_siji_shenfen)
    EditText et_siji_shenfen;
    @BindView(R.id.et_danhao_text)
    EditText et_danhao_text;




    @BindView(R.id.layout_operate_tip_dialog_titleTextView)
    TextView tv_title;


    @BindView(R.id.ll_phone)
    LinearLayout ll_phone;
    @BindView(R.id.ll_shenfen)
    LinearLayout ll_shenfen;
    @BindView(R.id.ll_danhao)
    LinearLayout ll_danhao;

    @BindView(R.id.iv_baoqi_select)
    ImageView iv_baoqi_select;

    //车队长
    @BindView(R.id.ll_chedui)
    LinearLayout ll_chedui;
    @BindView(R.id.iv_chedui_select)
    ImageView iv_chedui_select;


    @BindView(R.id.ll_chedui_phone)
    LinearLayout ll_chedui_phone;
    @BindView(R.id.et_chedui_phone)
    EditText et_chedui_phone;

    @BindView(R.id.ll_chedui_id)
    LinearLayout ll_chedui_id;
    @BindView(R.id.et_chedui_id)
    EditText et_chedui_id;






    private OrderInfo mOrderInfo;
    private boolean baoqiTrue=true;
    private boolean cheduiTrue=true;


    @Override
    public int getLayoutId() {
        return R.layout.activity_confirm_document;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("确认单据");

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
//            if (mOrderInfo != null) {
//
//            } else {
//                showEmptyView();
//            }
        }

        try {
            String string = getTime();
            siv_daohuo_time.setContent(string);
        }catch (Exception e){
            Log.e("fei",e.toString());
        }

//        mAppNameTextView.setText(AppUtils.getAppName());
//        mAppVersionTextView.setText("Version " + AppUtils.getAppVersionName());
//
//        if (BuildConfig.FLAVOR.equals("officer")) {
//            mUpdateLinearLayout.setVisibility(View.GONE);
//        } else {
//            mUpdateLinearLayout.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public BaseFragment createFragment() {
        return null;
    }

    @OnClick(R.id.layout_operate_tip_dialog_positiveTextView)
    public void clickTrue() {
        if (mOrderInfo != null) {
            queRenList(mOrderInfo);
        } else {
            showToast("获取数据失败");
        }

    }


    @OnClick(R.id.layout_operate_tip_dialog_negativeTextView)
    public void clickFalse() {
        finish();
    }

    @OnClick(R.id.siv_daohuo_time)
    public void clickTime() {
        selectTime();
    }


    @OnClick(R.id.ll_baoqi)
    public void selectbaoqi() {
        baoqiTrue=!baoqiTrue;

        if (baoqiTrue){
            iv_baoqi_select.setImageResource(R.mipmap.anniu_kai);
            ll_phone.setVisibility(View.VISIBLE);
            ll_shenfen.setVisibility(View.VISIBLE);
            ll_chedui.setVisibility(View.VISIBLE);
            ll_chedui_phone.setVisibility(View.VISIBLE);
            ll_chedui_id.setVisibility(View.VISIBLE);

            ll_danhao.setVisibility(View.GONE);
            siv_daohuo_time.setVisibility(View.GONE);

        }else {
            iv_baoqi_select.setImageResource(R.mipmap.anniu_guan);
            ll_phone.setVisibility(View.GONE);
            ll_shenfen.setVisibility(View.GONE);
            ll_chedui.setVisibility(View.GONE);
            ll_chedui_phone.setVisibility(View.GONE);
            ll_chedui_id.setVisibility(View.GONE);

            ll_danhao.setVisibility(View.VISIBLE);
            siv_daohuo_time.setVisibility(View.VISIBLE);

        }

        et_siji_phone.setText("");
        et_siji_shenfen.setText("");
        et_danhao_text.setText("");
        et_chedui_phone.setText("");
        et_chedui_id.setText("");

    }


    @OnClick(R.id.ll_chedui)
    public void selectCheDui() {
        cheduiTrue=!cheduiTrue;
        if (cheduiTrue){
            iv_chedui_select.setImageResource(R.mipmap.anniu_kai);
            ll_chedui_phone.setVisibility(View.VISIBLE);
            ll_chedui_id.setVisibility(View.VISIBLE);
        }else {
            iv_chedui_select.setImageResource(R.mipmap.anniu_guan);
            ll_chedui_phone.setVisibility(View.GONE);
            ll_chedui_id.setVisibility(View.GONE);
        }

        et_chedui_phone.setText("");
        et_chedui_id.setText("");


    }


    public void queRenList(OrderInfo orderInfo) {

                String driverName = tipTextView.getText().toString();
                String chePaiName = contentTextView.getText().toString();

                String phone = et_siji_phone.getText().toString();
                String shenfen = et_siji_shenfen.getText().toString();

                String danhao = et_danhao_text.getText().toString();
                String time = siv_daohuo_time.getContent();

                String cheduiPhone = et_chedui_phone.getText().toString();
                String cheduiId = et_chedui_id.getText().toString();


                if (TextUtils.isEmpty(driverName)||TextUtils.isEmpty(chePaiName)) {
                    ToastUtil.showShortToast("请输入车牌号或司机姓名");
                    return;
                }

                if (baoqiTrue) {
                    if (TextUtils.isEmpty(phone)) {
                        ToastUtil.showShortToast("请输入司机手机号");
                        return;
                    }
                    if (TextUtils.isEmpty(shenfen)) {
                        ToastUtil.showShortToast("请输入司机身份证号");
                        return;
                    }
                }
//                else {
//                    if (TextUtils.isEmpty(danhao)) {
//                        ToastUtil.showShortToast("请输入外部平台单号");
//                        return;
//                    }
//                    if (TextUtils.isEmpty(time)) {
//                        ToastUtil.showShortToast("请选择预计卸车时间");
//                        return;
//                    }
//                }

                Map<String, String> map = new HashMap<>();
                map.put("billno", orderInfo.billno);
                map.put("remarks", driverName);
                map.put("vehiclenum", chePaiName);


                map.put("drivertel", phone);
                map.put("driveridcard", shenfen);

                map.put("ymmorderno", danhao);
                if (baoqiTrue){
                    map.put("recelvetime", "");
                }else {
                    map.put("recelvetime", time);
                }


                map.put("agentPhone", cheduiPhone);
                map.put("agentldCardNumber", cheduiId);


//                                map.put("isstate", "11");
                new OrderRequestOperate().sureExternalRequest(ConfirmDocumentActivity.this, map,orderInfo.dingJinType ,
                        new OrderOperateCallback() {
                            @Override
                            public void operateCallback() {
                                ToastUtil.showShortToast("单据确认成功");
//                                if (isHasLoadData()) {
//                                    refreshData(false);
//                                }
                                EventBusManage.refreshOrderList();
//                                                EventBusManage.activityFinish();
                                finish();
                            }

                            @Override
                            public void onFailed(String message) {

                            }
                        });

            }




//        new OperateTipDialog.Builder(getActivity())
//                .title("请输入车牌号和司机姓名")
//                .input(new OperateTipDialog.InputCallback() {
//                    @Override
//                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
//                    }
//                })
//                .positiveText("确定")
//                .negativeText("取消")
//                .onPositive(new OperateTipDialog.SingleButtonCallback() {
//                    @Override
//                    public void onClick(@NonNull OperateTipDialog dialog) {
//                        String reason = dialog.getInputContent();
//                        if (TextUtils.isEmpty(reason)) {
//                            ToastUtil.showShortToast("请输入车牌号和司机姓名");
//                            return;
//                        }
//                        dialog.dismiss();
//                        Map<String, String> map = new HashMap<>();
//                        map.put("billno", orderInfo.billno);
//                        map.put("remarks", reason);
////                                map.put("isstate", "11");
//                        new OrderRequestOperate().sureExternalRequest(getActivity(), map,orderInfo.dingJinType ,
//                                new OrderOperateCallback() {
//                                    @Override
//                                    public void operateCallback() {
//                                        ToastUtil.showShortToast("单据确认成功");
//                                        if (isHasLoadData()) {
//                                            refreshData(false);
//                                        }
////                                                EventBusManage.refreshOrderList();
////                                                EventBusManage.activityFinish();
//                                    }
//
//                                    @Override
//                                    public void onFailed(String message) {
//
//                                    }
//                                });
//                    }
//                })
//                .show();





    private TimePickerView mTimePickerView;;
    private void selectTime() {
        hideKeyboard();
        if (mTimePickerView == null) {
            mTimePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd HH:mm:ss");
                    siv_daohuo_time.setContent(string);
                }
            })
                    .setType(TimePickerType.ALL)
                    .setTitleText("请选择司磅时间")
                    .build();
        }
        mTimePickerView.show();
    }


    private   String getTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区
        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
        String createDate = formatter.format(curDate);   //格式转换

        return createDate;
    }


}
